﻿using System;
using System.Drawing;

namespace MarioMapEditor
{
    class ObBrickSP : ObObject
    {
        public enum BRICKSP { BRICKSP1 = 40, BRICKSP2, BRICKSP3, BRICKSP4 };
        public enum INFO { NAM = 1, HOA, SAO, NAMHOA, TIEN, TIEN10, TIEN12, TIEN20, NAMLV};

        public ObBrickSP()
        {
            
        }

        public ObBrickSP(BRICKSP nid)
        {
            ID = (int)nid;
        }

        public ObBrickSP(BRICKSP nid, INFO nIndex)
        {
            ID = (int)nid;
            Info = (int)nIndex;
        }

        public override bool SetID(int nID)
        {
            if (checkType(nID))
            {
                ID = nID;
                return true;
            }
            return false;
        }

        public override bool SetINFO(int nInfo)
        {
            if (Enum.IsDefined(typeof(INFO), nInfo))
            {
                Info = nInfo;
                return true;
            }
            return false;
        }

        public static bool checkType(int objectID)
        {
            if (Enum.IsDefined(typeof(BRICKSP), objectID))
            {
                return true;
            }
            return false;
        }

        public static int selectBrick(int x, int y)
        {
            switch (x)
            {
                case 0:
                    if (y == 0)
                    {
                        return (int)BRICKSP.BRICKSP1;
                    }
                    if (y == 1)
                    {
                        return (int)BRICKSP.BRICKSP2;
                    }
                    if (y == 2)
                    {
                        return (int)BRICKSP.BRICKSP3;
                    }
                    return 0;
                case 1:
                    if (y == 0)
                    {
                        return (int)BRICKSP.BRICKSP4;
                    }
                    return 0;
                default:
                    return 0;
            }
        }

        public override Bitmap GetBitmap()
        {
            if (!checkType(ID))
            {
                return null;
            }
            var cropRect = new Rectangle(0, 0, MainForm.FileSize *Width, MainForm.FileSize * Height);
            switch (ID)
            {
                case (int)BRICKSP.BRICKSP1:
                    cropRect = new Rectangle(0, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICKSP.BRICKSP4:
                    cropRect = new Rectangle(MainForm.FileSize, 0, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICKSP.BRICKSP3:
                    cropRect = new Rectangle(0, MainForm.FileSize * 2, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
                case (int)BRICKSP.BRICKSP2:
                    cropRect = new Rectangle(0, MainForm.FileSize, MainForm.FileSize * Width, MainForm.FileSize * Height);
                    break;
            }

            var src = Properties.Resources.BrickSP;
            var trg = new Bitmap(Width * MainForm.TileSize, Height * MainForm.TileSize);
            using (var g = Graphics.FromImage(trg))
            {
                g.DrawImage(src, new Rectangle(0, 0, trg.Width, trg.Height), cropRect, GraphicsUnit.Pixel);
            }
            return trg;
        }
    }
}
